<?php
$auth = Auth::getAuth('current_user');
$db = new Database();
$company_id = $auth['company_id'];
$search = new Search();
$fs = new functions;
$form_id = $fs->base_encode_decode("decrypt", $_GET['id']);
$forms = $db->query("SELECT * FROM tb_workspace WHERE id = {$db->escape($form_id)}", "row");
$form_json = json_decode($forms['form_json']);
$fields_headerObj = $form_json->{'form_json'}->{'collected_data_header_info'}->{'listedchoice'};
$fields_headerObjType = $form_json->{'form_json'}->{'headerInfoType'};
$application_id = $_COOKIE['application'];
$user_id = $auth['id'];
// Set form customized print
$form_json = json_decode($forms['form_json'], true);
$form_json_customized_print = $search->getFormPrivacyUsers(json_encode($form_json['form_json']['customized-print']));
//get active fields
$active_fields = explode(",", $forms['active_fields']);
?>
<script type="text/javascript" src="/js/functions/user_view/getData.js"></script>
<br/>   
<h1><?php echo $forms['form_name']; ?></h1>
<?php
$form_name = str_replace(" ", "_",$forms['form_name']);
$formPrivacy_authors = $search->getFormPrivacyUsers($forms['form_authors']);
?>
<div class="fl-table-wrapper" form_id="<?php echo $form_id; ?>">
    <div class="fl-option-toolbar">
        <div class="fl-toolbar-left-wrapper">
            <ul>
                <?php
                    if($formPrivacy_authors){
                        echo '<li class="tour_button_create_request fl-buttonEffect"><a class="primary-btn" href="/user_view/workspace?view_type=request&formID='. $form_id .'&requestID=0">Create Request</a></li> ';
                        $import = '<li class="fl-buttonEffect"><a class="primary-btn" link-type ="import" link-form-id="'. $form_id .'">Import</a></li>';
                    }

                    if ($auth['user_level_id'] != "3") { 
                        echo $import;
                        // Get Generate Print Save from the database tb_generate
                        $getGeneratePrint = $db->query("SELECT * FROM tb_generate WHERE form_id={$db->escape($form_id)}", "row");
                        $getGeneratePrintNumrows = $db->query("SELECT * FROM tb_generate WHERE form_id={$db->escape($form_id)}", "numrows");
                        if ($getGeneratePrintNumrows == 0) {
                            if ($auth['user_level_id'] == "1" || $auth['user_level_id'] == "2" || $form_json_customized_print) {

                                echo ' <li class="fl-buttonEffect"><a href="/user_view/generate?view_type=edit&formID=' . $form_id . '" class="primary-btn">Customized Print</a></li>';
                            }
                        } else {
                            if ($auth['user_level_id'] == "1" || $auth['user_level_id'] == "2" || $form_json_customized_print) {

                                echo ' <li><a href="/user_view/generate?view_type=update&formID=' . $getGeneratePrint['id'] . '&printID=' . $getGeneratePrint['form_id'] . '" class="primary-btn">Update Print</a></li>';
                            }
                        }

                        if ($form_id == 234) {//aalisn din nten to jewel tolentino
                            if ($auth['user_level_id'] == "1" || $auth['user_level_id'] == "2") {
                                echo ' <li><a href="#" target-click="generate_payslip" class="primary-btn">Generate Payslip</a></li>';
                            }
                        }
                    }
                ?>
                
                <select class="fl-input-select  " id="request_buttons" style="width: 135px;">
                    <option value="0">Button Selection</option>
                    
                </select>
            </ul>
        </div>
        <div class="fl-toolbar-right-wrapper">
            <a class="pinit icon dataTip dashboard_pin" fl-data-application-id="<?php echo $application_id; ?>" fl-data-type="request" fl-data-object-id="<?php echo $form_id; ?>" data-placement="left" data-original-title="Pin this to Dashboard"></a>
        </div>
        <div class="clearfix"></div>
    </div><!-- end of option toolbar -->
    <div class="form-informtion display">
        <input type="text" class="display table-name" value="<?php echo $forms['form_table_name']; ?>">
        <input type="text" class="display form-name" value="<?php echo $forms['form_name']; ?>">
        <div class="form-header "><?php echo json_encode($fields_headerObj); ?></div>
        <input type="text" value="<?php echo $form_id; ?>" id="form_id" class="form_id">
        <?php
        $strSql = "SELECT DISTINCT COLUMN_NAME FROM information_schema.columns where table_name = '" . $forms['form_table_name'] . "' AND (DATA_TYPE = 'date' OR DATA_TYPE = 'datetime')";
        $getdateFields = $db->query($strSql, "array");
        $dateFields = array();
        foreach ($getdateFields as $value) {
            if (in_array($value['COLUMN_NAME'], $active_fields)) {
                // echo $value['COLUMN_NAME'];
                array_push($dateFields, $value['COLUMN_NAME']);
            }
        }
        ?>
        <select class="search-request-field_date display">
            <?php
            foreach ($dateFields as $value) {
                echo "<option value='" . $value . "'>" . $value . "</option>";
            }
            ?>
            <option value="DateCreated">DateCreated</option>
        </select>
    </div>
    <TABLE class="fl-search-cont" BORDER="5" WIDTH="100%" CELLPADDING="4" CELLSPACING="0" border="2">
        <TR ALIGN="CENTER" class="fl-header-tbl-wrapper">
            <TD colspan="5">
                <div class="fl-search-wrapper">
                    <!-- <form> -->
                    <input type="text"  placeholder="Search Requests" class="searchRequestUsersValue">
                    <input type="button" value="" class="search icon fl-btn-right searchRequestUsersButton" style="width:36px;position: relative; display:block;cursor:pointer">
                    <select class="fl-input-select search-request-field searchRequestUsersField display" style="margin-top:0px;width: 150px;margin-right:0px;padding-top: 5px;">
                        <!-- <option value="0">All</option> -->
                        <option value="TrackNo">Tracking Number</option>
                        <option value="Requestor">Requestor</option>
                        <option value="Processor">Processor</option>
                        <option value="Status">Status</option>
                        <?php
                        foreach ($active_fields as $value) {
                            echo "<option value='" . $value . "'>" . $value . "</option>";
                        }
                        ?>
                    </select>
                    <button class="tip btn-basicBtn padding_5 fl-basicsecondary-btn cursor fa fa-gear advanceFilterDialog" style="margin-left:4px; height: 31px;width: 30px;cursor:pointer" data-original-title="Advance Filter"></button>
                    <div class="fl-for-saving-fitler">
                        <ul>
                            <li class="display fl-buttonEffect filterAction fl-basicprimary-btn saveFC" id="saveFC">Save Filter</li>
                            <li class="display fl-buttonEffect filterAction fl-basicprimary-btn cancelFC" id="cancelFC">Cancel</li>
                            <div class="clearfix"></div>
                        </ul>
                    <!-- <input type="button" value="Save Filter" class="display filterAction" id="saveFC" style="margin-left: 5px;">
                    <input type="button" value="Cancel" class="display filterAction" id="cancelFC"> -->
                    </div>
                    <!-- </form>   -->
                </div>         

                <div class="fl-save-sort" style="float:right;position:relative;right: 5px;width: 175px;">
                    <select class="fl-input-select filterRequestsUser loadRF" id="loadRF" style="width: 135px;">
                        <option value="0" search_value="" search_field = "0">Filtered Request</option>
                        <?php
                        $strSql = "SELECT * FROM tbfilteredCategory WHERE user_id = '" . $user_id . "' && form_id = '" . $form_id . "'";
                        $getSaveFilter = $db->query($strSql, "array");
                        foreach ($getSaveFilter as $value) {
                            echo '<option value="' . $value['id'] . '" '. "multi_search='". $value['multi_search'] ."'" .' search_value="' . $value['search_value'] . '" search_field = "' . $value['search_field'] . '" date_field="' . $value['date_field'] . '" date_from="' . $value['date_from'] . '" date_to="' . $value['date_to'] . '">' . $value['title'] . '</option>';
                        }
                        ?>
                    </select>
                    <button class="tip btn-basicBtn padding_5 fl-basic-btn cursor requestFilterSettings fa fa-trash-o" style=" position:relative; top:-1px;height:31px;width: 30px;" data-original-title="Remove filter"></button>
                </div> 
            </TD>
        </TR>
    </TABLE>
    <!-- Data table for request -->
    <?php
    $getCustomView = $db->query("SELECT * FROM tbcustomview WHERE user_id = $user_id AND form_id = $form_id ", "array");

    if (count($getCustomView) > 0) {
        //default fields for records/request
        $customView = getValidHeaderFields($fields_headerObj, $getCustomView, $fields_headerObjType);
    } else {
        // for default view
        $customView = array(array("field_label" => "Tracking Number", "field_name" => "TrackNo"), array("field_label" => "Requestor", "field_name" => "requestorName"), array("field_label" => "Status", "field_name" => "Status"), array("field_label" => "Date Created", "field_name" => "DateCreated"));
        // push to array in specific location
        $otherFieldsStartPos = 2;
        foreach ($fields_headerObj as $fields_header) {
            array_insert($customView, array("field_label" => "" . $fields_header->{'field_label'} . "", "field_name" => "" . $fields_header->{'field_name'} . ""), $otherFieldsStartPos);
            $otherFieldsStartPos++;
        }

        //Updated by AARON TOLENTINO
        // - For having a specific and default header information

        if($fields_headerObjType=="1"){

            $customView = array();
            foreach ($fields_headerObj as $fields_header) {
                array_push($customView, array("field_label" => "" . $fields_header->{'field_label'} . "", "field_name" => "" . $fields_header->{'field_name'} . ""));
            }
        }else if($fields_headerObjType=="0"){
            // FOR DEFAULT COLUMN
            $customView = array(array("field_label" => "Tracking Number", "field_name" => "TrackNo"), array("field_label" => "Requestor", "field_name" => "requestorName"), array("field_label" => "Status", "field_name" => "Status"), array("field_label" => "Date Created", "field_name" => "DateCreated"));
        }
        $customView = json_decode(json_encode($customView), true);


        // print_r($customView);
    }
    ?>
    <!--OLD CHECKBOX FOR APP <input type='checkbox' data-type-form-name='" . $form_name . "' class='appCheckAll checkAllAction' /> -->
    <div class="fl-datatable-wrapper" style="width:100%; overflow:auto; overflow-y:hidden;">
        <table class="display_data dataTable recordsDatatable"  id="loadFormRecords" style="">
            <thead class="fl-header-tbl-wrapper">
                <tr>
                    <?php
                    echo "<th style='width:80px;'>" . "<div class='fl-table-ellip'>
                    <input type='checkbox' data-type-form-name='" . $form_name . "' name='checkboxG4' id='checkboxG4' class='css-checkbox appCheckAll checkAllAction' /><label for='checkboxG4' class='css-label'></label>
                                    </div></th>";
                    foreach ($customView as $value) {
                        if ($value['field_name'] != "") {
                            if ($value['width'] != "") {
                                $width = "width:" . $value['width'] . ";";
                            }
                            echo "<th style='" . $width . "' field_name='" . $value['field_name'] . "'>" . "<span class='sortable-image' style='position:absolute'></span><div class='fl-table-ellip'>" . $value['field_label'] . "</div></th>";
                        }
                    }
                    ?>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    <div class="dataTable_widget"></div>
</div>
<!-- end of table-->